<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css"/>
<style>
/* jstree 插件 */
.jstree {
  font-size: 1.2rem; }
  .jstree.jstree-default .jstree-clicked {
    background: transparent;
    box-shadow: none; }
</style>

<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <form id="my-form" class="am-form tpl-form-line-form" method="post">
                    <div class="widget-body">
                        <fieldset>
                            <div class="widget-head am-cf">
                                <div class="widget-title am-fl"><?= $info['username'] ?>授权</div>
                            </div>
                            <div class="am-form-group">
                                <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">分类权限 </label>
                                <div class="am-u-sm-9 am-u-end">
                                    <div id="jstree"></div>
                                </div>
                            </div>
                            <!-- <div class="am-form-group">
                                <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">分类权限 </label>
                                <div class="am-u-sm-9 am-u-end">
                                    <div id="jstree1"></div>
                                </div>
                            </div> -->
                            <div class="am-form-group">
                                <div class="am-u-sm-9 am-u-sm-push-3 am-margin-top-lg">
                                    <button type="submit" class="j-submit am-btn am-btn-secondary">提交
                                    </button>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="/static/assets/common/js/jstree.min.js"></script>
<script>
    $(function () {

        var $jstree = $('#jstree');
        $jstree.jstree({
            icon: false,
            plugins: ['checkbox'],
            core: {
                themes: {icons: false},
                checkbox: {
                    keep_selected_style: false
                },
                data: <?= $cateList ?>
            }
        });

        // var $jstree1 = $('#jstree1');
        // $jstree1.jstree({
        //     icon: false,
        //     plugins: ['checkbox'],
        //     core: {
        //         themes: {icons: false},
        //         checkbox: {
        //             keep_selected_style: false
        //         },
        //         data: <?= $cateList ?>
        //     }
        // });

        // 读取选中的条目
        $.jstree.core.prototype.get_all_checked = function (full) {
            var obj = this.get_selected(), i, j;
            for (i = 0, j = obj.length; i < j; i++) {
                obj = obj.concat(this.get_node(obj[i]).parents);
            }
            obj = $.grep(obj, function (v) {
                return v !== '#';
            });
            obj = obj.filter(function (itm, i, a) {
                return i === a.indexOf(itm);
            });
            return full ? $.map(obj, $.proxy(function (i) {
                return this.get_node(i);
            }, this)) : obj;
        };


        /**
         * 表单验证提交
         * @type {*}
         */
        $('#my-form').superForm({
            buildData: function () {
                return {
                    data: {
                        // city: $jstree.jstree('get_all_checked'),
                        cate: $jstree.jstree('get_all_checked')
                    }
                }
            }
        });

    });
</script>
